package view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.StringTokenizer;
import java.util.Vector;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.border.EmptyBorder;

import ui.components.BarChart;
import ui.components.Chart;
import ui.components.PieChart;

import filters.AndFilter;
import filters.BarrioFilter;
import filters.IFilter;
import filters.MesaFilter;
import filters.OrFilter;
import filters.SexFilter;
import filters.TotalFilter;

public class MetricGeneratorPanel extends JFrame {

	private JPanel contentPane;
	private MainView mView;
	private JTextField txtM1;
	private JTextField txtM2;
	private JTextField textNombreMetrica;

	/**
	 * Launch the application.
	 */

	/**
	 * Create the frame.
	 */
	public MetricGeneratorPanel(final MainView mView) {
		setResizable(false);
		this.mView = mView;
		this.setTitle("Generador de Metricas");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 662, 239);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		final JButton btnAceptarMetric = new JButton("Aceptar");
		btnAceptarMetric.setEnabled(false);
		
		JPanel panel = new JPanel();
		panel.setBounds(47, 11, 546, 144);
		contentPane.add(panel);
		panel.setLayout(null);

		JPanel panel_1 = new JPanel();
		panel_1.setLayout(null);
		panel_1.setBounds(362, 34, 174, 103);
		panel.add(panel_1);
		
		final JComboBox cS2 = new JComboBox();
		cS2.setEnabled(false);
		cS2.setModel(new DefaultComboBoxModel(new String[] {"None", "Masculino", "Femenino"}));
		cS2.setBounds(77, 28, 86, 20);
		panel_1.add(cS2);
		
		JPanel primaryFilterContainer = new JPanel();
		primaryFilterContainer.setBounds(10, 34, 174, 103);
		panel.add(primaryFilterContainer);
		primaryFilterContainer.setLayout(null);
		
		final JTextPane txtB1 = new JTextPane();
		txtB1.setBounds(77, 59, 86, 20);
		primaryFilterContainer.add(txtB1);
		
		final JComboBox cS1 = new JComboBox();
		cS1.setBounds(77, 28, 86, 20);
		primaryFilterContainer.add(cS1);
		cS1.setModel(new DefaultComboBoxModel(new String[] {"None", "Masculino", "Femenino"}));
		
		final JComboBox cTipoChart = new JComboBox();
		contentPane.add(cTipoChart);
		cTipoChart.setModel(new DefaultComboBoxModel(new String[] {"", "Barras", "Torta"}));
		
		JLabel lblMesas = new JLabel("Mesas");
		lblMesas.setBounds(0, 3, 67, 14);
		primaryFilterContainer.add(lblMesas);
		
		txtM1 = new JTextField();
		txtM1.setToolTipText("Ingrese los nros de mesa separadas por \",\"");
		txtM1.setBounds(77, 3, 86, 20);
		primaryFilterContainer.add(txtM1);
		txtM1.setColumns(10);
		
		JLabel lblSexo = new JLabel("Sexo");
		lblSexo.setBounds(0, 28, 67, 14);
		primaryFilterContainer.add(lblSexo);
		JLabel lblBarrio = new JLabel("Barrio");
		lblBarrio.setBounds(0, 59, 67, 14);
		primaryFilterContainer.add(lblBarrio);
		
		JLabel label = new JLabel("Mesas");
		label.setBounds(0, 3, 67, 14);
		panel_1.add(label);
		
		txtM2 = new JTextField();
		txtM2.setEnabled(false);
		txtM2.setColumns(10);
		txtM2.setBounds(77, 3, 86, 20);
		panel_1.add(txtM2);
		
		JLabel label_1 = new JLabel("Sexo");
		label_1.setBounds(0, 28, 67, 14);
		panel_1.add(label_1);
		JLabel label_2 = new JLabel("Barrio");
		label_2.setBounds(0, 59, 67, 14);
		panel_1.add(label_2);
		
		JLabel lblTipoDeFiltro = new JLabel("Tipo de Combinaci\u00F3n");
		lblTipoDeFiltro.setBounds(216, 34, 122, 14);
		panel.add(lblTipoDeFiltro);
		
		final JComboBox cFiltro = new JComboBox();
		cFiltro.setBounds(216, 54, 119, 20);
		panel.add(cFiltro);
		cFiltro.setModel(new DefaultComboBoxModel(new String[] {"", "AND", "OR"}));
		
		
		cS1.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent arg0) {
				if((String)cS1.getSelectedItem()!="None"){
					txtM1.enable(false);
					txtB1.enable(false);}
				else
					{txtM1.enable(true);
					txtB1.enable(true);}	
					
				}
			
		});

		final JTextPane txtB2 = new JTextPane();
		txtB2.setEnabled(false);
		txtB2.setText("");
		txtB2.setBounds(77, 53, 86, 20);
		panel_1.add(txtB2);
		
		final JCheckBox ninguno = new JCheckBox("Ninguno");
		
		ninguno.setBounds(10, 7, 97, 23);
		panel.add(ninguno);
		
		btnAceptarMetric.setBounds(547, 166, 89, 29);
		contentPane.setLayout(null);
		contentPane.add(btnAceptarMetric);
		
		cFiltro.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
					if (txtM1.getText().equals(""))
						txtM2.setEnabled(true);
					
					cS2.setEnabled(true);
					txtB2.setEnabled(true);
					
					txtM1.setEnabled(false);
					txtB1.setEnabled(false);
					cS1.setEnabled(false);
					
					
			}
		});
		
		cTipoChart.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent arg0) {
				btnAceptarMetric.setEnabled(true);}
		});
		
		cTipoChart.setBounds(308, 170, 119, 20);
	
		
		JLabel lblVisualizacin = new JLabel("Tipo de Grafico");
		lblVisualizacin.setBounds(220, 173, 107, 14);
		contentPane.add(lblVisualizacin);
		
		JSeparator separator = new JSeparator();
		separator.setBounds(0, 154, 636, 14);
		contentPane.add(separator);
		
		JLabel lblNombreMetrica = new JLabel("Nombre Metrica");
		lblNombreMetrica.setBounds(10, 173, 97, 14);
		contentPane.add(lblNombreMetrica);
		
		textNombreMetrica = new JTextField();
		textNombreMetrica.setBounds(105, 170, 107, 20);
		contentPane.add(textNombreMetrica);
		textNombreMetrica.setColumns(10);
		
		txtM1.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent arg0) {
				if (!txtM1.getText().equals(""))
					{txtB1.setEnabled(false);
					cS1.setEnabled(false);}
				else
					{txtB1.setEnabled(true);
					cS1.setEnabled(true);}
			}
		});
		
		
		txtB1.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent arg0) {
				if (!txtB1.getText().equals(""))
					{txtM1.setEnabled(false);
					cS1.setEnabled(false);}
				else
					{txtM1.setEnabled(true);
					cS1.setEnabled(true);}
			}
		});
		
		txtM1.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent arg0) {
				if (!txtM1.getText().equals(""))
					{txtB1.setEnabled(false);
					cS2.setEnabled(false);}
				else
					{txtB1.setEnabled(true);
					cS2.setEnabled(true);}
			}
		});
		
		
		txtB2.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent arg0) {
				if (!txtB2.getText().equals(""))
					{if (!txtM1.getText().equals(""))
						 txtM2.setEnabled(false);
					else
						txtM2.setEnabled(true);
					cS2.setEnabled(false);}
				else
					{if (!txtM1.getText().equals(""))
						txtM2.setEnabled(false);
					else
						txtM2.setEnabled(true);
					 	cS2.setEnabled(true);}
			}
		});
		
		txtM2.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent arg0) {
				if (!txtM2.getText().equals(""))
					{txtB2.setEnabled(false);
					cS2.setEnabled(false);}
				else
					{txtB2.setEnabled(true);
					cS2.setEnabled(true);}
			}
		});
		ninguno.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent arg0) {
				if (ninguno.isSelected())
				{
					cS1.setSelectedIndex(-1);
					cS2.setSelectedIndex(-1);
					cFiltro.setSelectedIndex(-1);
					txtM1.setEnabled(false);
					txtB1.setEnabled(false);
					cS1.setEnabled(false);
					cFiltro.setEnabled(false);
					txtM2.setEnabled(false);
					cS2.setEnabled(false);
					txtB2.setEnabled(false);
				}
				else
					{
					cTipoChart.setSelectedIndex(-1);
					cS1.setSelectedIndex(-1);
					cS2.setSelectedIndex(-1);
					cFiltro.setSelectedIndex(-1);
					txtM1.setEnabled(true);
					txtM1.setText("");
					txtB1.setEnabled(true);
					txtB1.setText("");
					cS1.setEnabled(true);
				 	btnAceptarMetric.setEnabled(false);
					txtM2.setEnabled(false);
					txtM2.setText("");
					cS2.setEnabled(false);
					txtB2.setEnabled(false);
					txtB2.setText("");
					ninguno.setSelected(false);
					cFiltro.setEnabled(true);
					
					textNombreMetrica.setText("");}
				}
		});
		btnAceptarMetric.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				mView.addChart(generateChart());
				if(ninguno.isSelected()){
					mView.addFilter(new TotalFilter());
				}else{
					Integer selected1 = getSelected(txtM1, cS1 , txtB1);
					Integer selected2 = getSelected(txtM2, cS2 , txtB2);
					if(selected2 == 4){
						mView.addFilter(getSimpleFilter(selected1,txtM1,cS1,txtB1));
					}else{
						mView.addFilter(getComplexFilter(selected1,selected2));
					}
				}					
						
				//seteo botones ventanita estado original
				txtM1.setEnabled(true);
				txtM1.setText("");
				txtB1.setEnabled(true);
				txtB1.setText("");
				cS1.setEnabled(true);
			 	btnAceptarMetric.setEnabled(false);
				txtM2.setEnabled(false);
				txtM2.setText("");
				cS2.setEnabled(false);
				txtB2.setEnabled(false);
				txtB2.setText("");
				ninguno.setSelected(false);
				cFiltro.setEnabled(true);
				cTipoChart.setSelectedIndex(-1);
				textNombreMetrica.setText("");
				
			}
			
			 private IFilter getComplexFilter(Integer selected1,Integer selected2) {
				 IFilter aux = null;
				 IFilter a = getSimpleFilter(selected1,txtM1,cS1,txtB1);
				 if(a==null)
					 System.out.println("A es null");
				 IFilter b = getSimpleFilter(selected2,txtM2,cS2,txtB2);				 
				 if(cFiltro.getSelectedItem().toString().equals("AND")){
					 aux = new AndFilter(a,b);
				 }else{
					 aux = new OrFilter(a,b);
				 }
				return aux;
			}

			private IFilter getSimpleFilter(Integer selected1,JTextField mesa,JComboBox sexo,JTextPane barrio) {
				IFilter aux = null;
				switch(selected1){
				case 1:
					aux = generateMesasFilters(mesa);
					break;
				case 2:
					aux = new SexFilter(sexo.getSelectedItem().toString().equals("Masculino"));
					break;
				case 3:
					aux = new BarrioFilter(barrio.getText());
					break;
				}				
				return aux;
			}

			private IFilter generateMesasFilters(JTextField txtM1) {				
					 String s=txtM1.getText();
					 StringTokenizer st=new StringTokenizer(s,",");
					 Vector<MesaFilter> mesas =new Vector<MesaFilter>();
					 while (st.hasMoreTokens()){
						 MesaFilter aux= new MesaFilter(Integer.valueOf(st.nextToken()));
						 mesas.add(aux);}
					 IFilter filter1=mesas.elementAt(0);
					 mesas.remove(0);
					 while(mesas.size()>0){
						 IFilter or=new OrFilter(filter1, mesas.elementAt(0));
						 filter1=or;
						 if(mesas.size()>0)
							 mesas.remove(0);}
					 return filter1;
			}

			private Chart generateChart(){
				if (cTipoChart.getSelectedIndex()==1)
					return new BarChart(textNombreMetrica.getText());
				else
					return new PieChart(textNombreMetrica.getText());				
			 }
			 
			 private Integer getSelected(JTextField mesa,JComboBox sexo,JTextPane barrio){
				 if(!mesa.getText().equals(""))
					 return 1;
				 else if (!sexo.getSelectedItem().toString().equals("None"))
					 return 2;
				 else if (!barrio.getText().equals(""))
					 return 3;
				 else
					 return 4;
			 }		 
			 
		});
	}	
}
